package com.tecode.gmail.user.controller;

import com.tecode.gmail.bean.GoodsClassify;
import com.tecode.gmail.bean.User;
import com.tecode.gmail.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created with Intellij IDEA.
 *
 * @Author: Gabriel
 * @Date: 2019-01-03
 * @Description:
 */
@RequestMapping("/user")
@Controller
public class UserController {
	@Autowired
	private UserService userService;

	/**
	 * @Author Gabriel
	 * @Description //TODO
	 * @Date 2019/1/3
	 * @Param
	 * @Return
	 * 登录方法
	 */
	@ResponseBody
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public Map<String,Object> login(User user, HttpSession session){
		Map<String,Object> map = userService.login(user);
		if(Boolean.parseBoolean(map.get("result").toString())){
			session.setAttribute("loginUser",map.get("loginUser"));
		}
		return map;
	}




	/**
	 * @Author Gabriel
	 * @Description //TODO
	 * @Date 2019/1/3
	 * @Param
	 * @Return
	 * 发送验证码方法
	 */
	@ResponseBody
	@RequestMapping("/sendSMS/{phone}")
	public Map<String,Object> sendSMS(@PathVariable("phone") String phone){
		return userService.sendSMS(phone);
	}


	/**
	 * 退出登录方法
	 * @param session
	 * @return
	 */
	@RequestMapping("/logOut")
	public String logOut(HttpSession session){
		session.invalidate();
		return "redirect:/html/login.html";
	}

	/**
	 * 用户购物车请求方法
	 * @return
	 */
	@RequestMapping("/shopcart.html")
	public ModelAndView userShopcart(){
		ModelAndView modelAndView = new ModelAndView("html/shopcart");

		return modelAndView;
	}


	/**
	 * 用户登录页面
	 * @return
	 */
	@RequestMapping("/login.html")
	public ModelAndView userLogin(){
		ModelAndView modelAndView = new ModelAndView("html/login");

		return modelAndView;
	}
}